home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / asm-mode.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  7.4 KB  |  234 lines

  1. ;;; asm-mode.el --- mode for editing assembler code
  2.  
  3. ;; Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Last-Modified: 14 Jul 1992
  7. ;; Keywords: tools, languages
  8.  
  9. ;;     @(#)asm-mode.el    1.7
  10.  
  11. ;; This file is part of XEmacs.
  12.  
  13. ;; XEmacs is free software; you can redistribute it and/or modify it
  14. ;; under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; XEmacs is distributed in the hope that it will be useful, but
  19. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. ;; General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  25. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;;; Synched up with: FSF 19.28.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; This mode was written by Eric S. Raymond <esr@snark.thyrsus.com>,
  32. ;; inspired by an earlier asm-mode by Martin Neitzel.
  33.  
  34. ;; This minor mode is based on text mode.  It defines a private abbrev table
  35. ;; that can be used to save abbrevs for assembler mnemonics.  It binds just
  36. ;; five keys:
  37. ;;
  38. ;;    TAB        tab to next tab stop
  39. ;;    :        outdent preceding label, tab to tab stop
  40. ;;    ;        place or move comment
  41. ;;    C-j, C-m    newline and tab to tab stop
  42. ;;
  43. ;; Code is indented to the first tab stop level.
  44. ;; The ; key inserts copies of the value of asm-comment-char at an
  45. ;; appropriate spot.
  46.  
  47. ;; This mode runs two hooks:
  48. ;;   1) An asm-set-comment-hook before the part of the initialization
  49. ;; depending on asm-comment-char, and
  50. ;;   2) an asm-mode-hook at the end of initialization.
  51.  
  52. ;;; Code:
  53.  
  54. (defvar asm-comment-char ?;
  55.   "*The comment-start character assumed by Asm mode.")
  56.  
  57. (defvar asm-suport-c-comments-p t
  58.   "*Support C style comments.  If t C style comments will be
  59. supported.  This is mainly for the benefit of font-lock")
  60.  
  61. (defvar asm-mode-syntax-table nil
  62.   "Syntax table used while in Asm mode.")
  63.  
  64. (defvar asm-mode-abbrev-table nil
  65.   "Abbrev table used while in Asm mode.")
  66. (define-abbrev-table 'asm-mode-abbrev-table ())
  67.  
  68. (defvar asm-mode-map nil
  69.   "Keymap for Asm mode.")
  70.  
  71. (if asm-mode-map
  72.     nil
  73.   (setq asm-mode-map (make-sparse-keymap))
  74.   (set-keymap-name asm-mode-map 'asm-mode-map)
  75.   (define-key asm-mode-map ";"        'asm-comment)
  76.   (define-key asm-mode-map ":"        'asm-colon)
  77.   (define-key asm-mode-map "\C-i"    'tab-to-tab-stop)
  78.   (define-key asm-mode-map "\C-j"    'asm-newline)
  79.   (define-key asm-mode-map "\C-m"    'asm-newline)
  80.   )
  81.  
  82. (defvar asm-code-level-empty-comment-pattern nil)
  83. (defvar asm-flush-left-empty-comment-pattern nil)
  84. (defvar asm-inline-empty-comment-pattern nil)
  85.  
  86. ;;;###autoload
  87. (defun asm-mode ()
  88.   "Major mode for editing typical assembler code.
  89. Features a private abbrev table and the following bindings:
  90.  
  91. \\[asm-colon]\toutdent a preceding label, tab to next tab stop.
  92. \\[tab-to-tab-stop]\ttab to next tab stop.
  93. \\[asm-newline]\tnewline, then tab to next tab stop.
  94. \\[asm-comment]\tsmart placement of assembler comments.
  95.  
  96. The character used for making comments is set by the variable
  97. `asm-comment-char' (which defaults to `?;').
  98.  
  99. Alternatively, you may set this variable in `asm-set-comment-hook', which is
  100. called near the beginning of mode initialization.
  101.  
  102. Turning on Asm mode runs the hook `asm-mode-hook' at the end of initialization.
  103.  
  104. Special commands:\\{asm-mode-map}
  105. "
  106.   (interactive)
  107.   (kill-all-local-variables)
  108.   (use-local-map asm-mode-map)
  109.   (setq mode-name "Assembler")
  110.   (setq major-mode 'asm-mode)
  111.   (setq local-abbrev-table asm-mode-abbrev-table)
  112.   (make-local-variable 'asm-mode-syntax-table)
  113.   (setq asm-mode-syntax-table (make-syntax-table))
  114.   (set-syntax-table asm-mode-syntax-table)
  115.   (run-hooks 'asm-mode-set-comment-hook)
  116.   (if asm-suport-c-comments-p
  117.       (progn
  118.     (modify-syntax-entry ?/ ". 14" asm-mode-syntax-table)
  119.     (modify-syntax-entry ?* ". 23" asm-mode-syntax-table)
  120.     (modify-syntax-entry asm-comment-char "< b" asm-mode-syntax-table)
  121.     (modify-syntax-entry ?\n "> b" asm-mode-syntax-table))
  122.     (progn
  123.       (modify-syntax-entry asm-comment-char
  124.                "<" asm-mode-syntax-table)
  125.       (modify-syntax-entry ?\n
  126.                ">" asm-mode-syntax-table)))
  127.       
  128.   (let ((cs (regexp-quote (char-to-string asm-comment-char))))
  129.     (make-local-variable 'comment-start)
  130.     (setq comment-start (concat cs " "))
  131.     (make-local-variable 'comment-start-skip)
  132.     (setq comment-start-skip (concat cs "+[ \t]*"))
  133.     (setq asm-inline-empty-comment-pattern (concat "^.+" cs "+ *$"))
  134.     (setq asm-code-level-empty-comment-pattern (concat "^[\t ]+" cs cs " *$"))
  135.     (setq asm-flush-left-empty-comment-pattern (concat "^" cs cs cs " *$"))
  136.     )
  137.   (make-local-variable 'comment-end)
  138.   (setq comment-end "")
  139.   (make-local-variable 'comment-column)
  140.   (setq comment-column 32)
  141.   (setq fill-prefix "\t")
  142.   (run-hooks 'asm-mode-hook)
  143.   )
  144.  
  145.  
  146. (defun asm-colon ()
  147.   "Insert a colon; if it follows a label, delete the label's indentation."
  148.   (interactive)
  149.   (save-excursion
  150.     (beginning-of-line)
  151.     (if (looking-at "[ \t]+\\(\\sw\\|\\s_\\)+$")
  152.     (delete-horizontal-space)))
  153.   (insert ":")
  154.   (tab-to-tab-stop)
  155.   )
  156.  
  157. (defun asm-newline ()
  158.   "Insert LFD + fill-prefix, to bring us back to code-indent level."
  159.   (interactive)
  160.   (if (eolp) (delete-horizontal-space))
  161.   (insert "\n")
  162.   (tab-to-tab-stop)
  163.   )
  164.  
  165. (defun asm-line-matches (pattern &optional withcomment)
  166.   (save-excursion
  167.     (beginning-of-line)
  168.     (looking-at pattern)))
  169.  
  170. (defun asm-pop-comment-level ()
  171.   ;; Delete an empty comment ending current line.  Then set up for a new one,
  172.   ;; on the current line if it was all comment, otherwise above it
  173.   (end-of-line)
  174.   (delete-horizontal-space)
  175.   (while (= (preceding-char) asm-comment-char)
  176.     (delete-backward-char 1))
  177.   (delete-horizontal-space)
  178.   (if (bolp)
  179.       nil
  180.     (beginning-of-line)
  181.     (open-line 1))
  182.   )
  183.  
  184.  
  185. (defun asm-comment ()
  186.   "Convert an empty comment to a `larger' kind, or start a new one.
  187. These are the known comment classes:
  188.  
  189.    1 -- comment to the right of the code (at the comment-column)
  190.    2 -- comment on its own line, indented like code
  191.    3 -- comment on its own line, beginning at the left-most column.
  192.  
  193. Suggested usage:  while writing your code, trigger asm-comment
  194. repeatedly until you are satisfied with the kind of comment."
  195.   (interactive)
  196.   (cond
  197.  
  198.    ;; Blank line?  Then start comment at code indent level.
  199.    ((asm-line-matches "^[ \t]*$")
  200.     (delete-horizontal-space)
  201.     (tab-to-tab-stop)
  202.     (insert asm-comment-char comment-start))
  203.  
  204.    ;; Nonblank line with no comment chars in it?
  205.    ;; Then start a comment at the current comment column
  206.    ((asm-line-matches (format "^[^%c\n]+$" asm-comment-char))
  207.     (indent-for-comment))
  208.  
  209.    ;; Flush-left comment present?  Just insert character.
  210.    ((asm-line-matches asm-flush-left-empty-comment-pattern)
  211.     (insert asm-comment-char))
  212.  
  213.    ;; Empty code-level comment already present?
  214.    ;; Then start flush-left comment, on line above if this one is nonempty. 
  215.    ((asm-line-matches asm-code-level-empty-comment-pattern)
  216.     (asm-pop-comment-level)
  217.     (insert asm-comment-char asm-comment-char comment-start))
  218.  
  219.    ;; Empty comment ends line?
  220.    ;; Then make code-level comment, on line above if this one is nonempty. 
  221.    ((asm-line-matches asm-inline-empty-comment-pattern)
  222.     (asm-pop-comment-level)
  223.     (tab-to-tab-stop)
  224.     (insert asm-comment-char comment-start))
  225.  
  226.    ;; If all else fails, insert character
  227.    (t
  228.     (insert asm-comment-char))
  229.  
  230.    )
  231.   (end-of-line))
  232.  
  233. ;;; asm-mode.el ends here
  234.